﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using Microsoft.Office.Project.EPMAuditing.Library;
using System.IO;

namespace Microsoft.Office.Project.EPMAuditing.AuditListenerCatalog
{
    public class TextFileAuditListener : BaseFileAuditListener
    {
        protected override string NewFileName()
        {
            return String.Format(CultureInfo.InvariantCulture, "{0}{1} {2}.txt", FileDirectory, FileBaseName,
                DateTime.Now.ToString("yyyyMMdd-HHmmss", DateTimeFormatInfo.InvariantInfo));
        }

        public override void Write(MemoryStream ms)
        {
            //ChangeRecord changeRecord = DeserializeMessage(ms);
            ChangeRecord changeRecord = new ChangeRecord(ms);
            changeRecord.TracingService = TracingService;

            base.WriteToFile(String.Format(CultureInfo.InvariantCulture, "Event Time:        {0}", changeRecord.EventTime));
            base.WriteToFile(String.Format(CultureInfo.InvariantCulture, "Event Name:        {0}", changeRecord.EventName));
            base.WriteToFile(String.Format(CultureInfo.InvariantCulture, "Event Source Name: {0}", changeRecord.EventSourceName));
            base.WriteToFile(String.Format(CultureInfo.InvariantCulture, "Is a Windows User: {0}", changeRecord.EventContextInfo.IsWindowsUser.ToString()));
            base.WriteToFile(String.Format(CultureInfo.InvariantCulture, "User Name:         {0}", changeRecord.EventContextInfo.UserName));
            base.WriteToFile(String.Format(CultureInfo.InvariantCulture, "User GUID:         {0}", changeRecord.EventContextInfo.UserGuid.ToString()));
            base.WriteToFile(String.Format(CultureInfo.InvariantCulture, "Tracking GUID:     {0}", changeRecord.EventContextInfo.TrackingGuid.ToString()));
            base.WriteToFile(String.Format(CultureInfo.InvariantCulture, "Site GUID:         {0}", changeRecord.EventContextInfo.SiteGuid.ToString()));
            base.WriteToFile(String.Format(CultureInfo.InvariantCulture, "LCID:              {0}", changeRecord.EventContextInfo.Lcid));
            base.WriteToFile(String.Format(CultureInfo.InvariantCulture, "PSEventArgs:       {0}", changeRecord.PSEventArgsXml));
            base.WriteToFile(String.Format(CultureInfo.InvariantCulture, "DataSetChanges:    {0}", changeRecord.DataSetChanges));
            base.WriteToFile("-----------------------------------------------------------------------------------------------------------------------");
        }

        protected override void AddHeader()
        {
            base.WriteToFile(string.Format(CultureInfo.InvariantCulture, "File opened at {0}", DateTime.Now.ToString()));
            base.WriteToFile("-----------------------------------------------------------------------------------------------------------------------");
        }

        protected override void AddFooter()
        {
            base.WriteToFile(string.Format(CultureInfo.InvariantCulture, "File closed at {0}", DateTime.Now.ToString()));
        }
    }
}
